
const db = wx.cloud.database()

Page({
  data: {
    //模糊时长
    timer:0,
    // // 是否隐藏模糊查询的面板
    hideScroll:true,
    // 模糊查询结果
    searchTip:[]
  },

  onLoad: function (options) {
    this.setData({
      list: wx.getStorageSync('searchHis')
    })
  },
  onShow: function () {

  },
  search(e) {
    this.setData({
      inputVal: e.detail
    })

    if (e.detail) {
      let searchHis = wx.getStorageSync('searchHis')
      if (!searchHis) {
        searchHis = [e.detail]
      }
      if (!searchHis.includes(e.detail)) {
        searchHis.push(e.detail)
      }
      wx.setStorageSync('searchHis', searchHis)
      this.setData({
        list: searchHis
      })
    }
    wx.redirectTo({
      url: '/pages/goods/list?name=' + this.data.inputVal,
    })
  },

  compkey(e){
    this.setData({
      inputVal: e.detail
    })
    console.log("输出")
    // clearTimeout(this.data.timer)
    // let timer = setTimeout(() => {
    //   if (inputVal) {
    //     // 如果输入的关键字不为空，则发起请求获取联想值
    //     const tips = [
    //       { tip: '1', content: '2' },
    //       { tip: '2', content: '2' },
    //       { tip: '3', content: '3' },
    //       { tip: '3', content: '3' },
    //       { tip: '3', content: '3' },
    //       { tip: '3', content: '3' },
    //       { tip: '3', content: '3' },
    //       { tip: '3', content: '3' }
    //     ]

    //     const newTips = tips.map(item => {
    //       const { tip, content } = item

    //       const newContent = this.getInf(content, inputVal)

    //       return { tip, content: newContent }
    //     })

    //     console.log('newTips:', newTips)

    //     this.setData({
    //       inputVal: inputVal,
    //       searchTip: newTips,
    //       hideScroll: false
    //     })
    //     return
    //   }
    //   // 如果为空，则收起
    //   this.setData({
    //     searchTip: [],
    //     hideScroll: true,
    //     inputVal: ''
    //   })
    // }, 600)

    // this.data.timer = timer
  },
  onClose(e) {
    const idx = e.currentTarget.dataset.idx
    const searchHis = this.data.list
    searchHis.splice(idx, 1)
    wx.setStorageSync('searchHis', searchHis)
    this.setData({
      list: searchHis
    })
  },

  go(e) {
    const idx = e.currentTarget.dataset.idx
    const keywords = this.data.list[idx]
    wx.redirectTo({
      url: '/pages/goods/list?name=' + keywords,
    })
  },

  getInf(str, key) {
    return str
      .replace(new RegExp(`${key}`, 'g'), `%%${key}%%`)
      .split('%%')
      .filter(item => {
        if (item) {
          return true
        }
        return false
      })
  },
  
  itemtap(e) {
    const { info } = e.currentTarget.dataset

    this.setData({
      // 将点击选择的值展示在input框中
      inputVal: info.content.join(''),
      // 当用户选择某个联想词，隐藏下拉列表
      hideScroll: true
    })
    this.addHistorySearch(info)
    // 发起请求，获取查询结果
    this.searchByKeyWord(info)
  },
  searchByKeyWord(info) {
    // 发起请求，获取面板数据
  },
  
  addHistorySearch(value) {
    const historySearch = wx.getStorageSync('historySearch') || []
    // 是否有重复的历史记录

    let has = false

    for (let history of historySearch) {
      const { content } = history

      if (value.content === content) {
        has = true
        break
      }
    }

    if (has) {
      return
    }

    const len = historySearch.length

    if (len >= 8) {
      historySearch.pop()
    }

    historySearch.unshift(value)
    wx.setStorage({
      key: 'historySearch',
      data: historySearch,
      success: () => {
        this.setData({ historySearch: historySearch })
      }
    })
  },
 
  getSum(){
    wx.cloud.callFunction({
      name:"comoKcloud"
    })
  
  db.collection('todos').where({
    "seed":"图片"
  }).get().then(res =>{
    this.setData({
      info:res.data
    })
  }).then(res =>{

  })
  },
  onTapCompKey:function(event){
    wx.navigateTo({
      url:"/pages/test/test"
    })
  }
})